package com.isoftstone.project.system.service.impl;


import com.isoftstone.framework.domain.Page;
import com.isoftstone.project.system.domain.SysMenu;
import com.isoftstone.project.system.mapper.SysMenuMapper;
import com.isoftstone.project.system.mapper.SysRoleAppMapper;
import com.isoftstone.project.system.service.ISysMenuService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class SysMenuService implements ISysMenuService {

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Autowired
    SysRoleAppMapper appMapper;


    public SysMenu getMenuById(String menuId) throws Exception {
        return sysMenuMapper.getMenuById(menuId);
    }

    public SysMenu getMenuByPermission(String permission) throws Exception {
        return sysMenuMapper.getMenuByPermission(permission);
    }

    public List<SysMenu> getMenulistPage(Page page) throws Exception {
        return sysMenuMapper.getMenulistPage(page);
    }

    public List<SysMenu> getMenuAll(String appId) throws Exception {
        return sysMenuMapper.getMenuAll(appId);
    }

    @Transactional
    public void saveSysMenu(SysMenu menu) throws Exception {
        sysMenuMapper.saveSysMenu(menu);
    }
    @Transactional
    public void updateSysMenu(SysMenu menu) throws Exception {
        sysMenuMapper.updateSysMenu(menu);
    }

    @Transactional
    public void deleteSysMenu(String menuId) throws Exception {
        sysMenuMapper.deleteSysMenu(menuId);
    }

    public List<SysMenu> getMenuByUrl(String userId) throws Exception {

        List<SysMenu> menuList = sysMenuMapper.getMenusByUserId(userId);
        for (SysMenu menu : menuList) {
            menu.setSysMenus(this.getMenuByParentIdRoleId(menu.getMenuId(),userId));
        }

        return menuList;
    }

    @Override
    public List<String> getMenuByUserId(String userId) throws Exception {
        return sysMenuMapper.getMenuByUserId(userId);
    }

    public List<SysMenu> getMenuByRoleId(String roleId) throws Exception {
        return sysMenuMapper.getMenuByRoleId(roleId);
    }

    @Override
    public List<SysMenu> getMenuByParentId(String parentId) throws Exception {
        return sysMenuMapper.getMenuByParentId(parentId);
    }

    public List<SysMenu> getMenuByParentIdRoleId(String parentId,String userId) throws Exception {
        List<SysMenu> menuList = sysMenuMapper.getMenuByParentIdRoleId(parentId,userId);
        for (SysMenu menu : menuList) {
            menu.setSysMenus(this.getMenuByParentIdRoleId(menu.getMenuId(),userId));
        }
        return menuList;
    }
}
